package com.hz.ys_clientorder.action;


import java.util.ArrayList;
import java.util.List;

import com.hz.framework.MyAction.BaseAction;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.CommonUtils;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.Numbers;
import com.hz.ys_clientorder.bean.Clientinfo;
import com.hz.ys_clientorder.bean.MuiltTaskinfo;
import com.hz.ys_clientorder.bean.Orderinfo;
import com.hz.ys_clientorder.bean.Taskinfo;
import com.hz.ys_clientorder.dao.OrderDao;
import com.hz.ys_financial.bean.Takingsorpay;
import com.hz.ys_financial.dao.FinancialDao;
import com.hz.ys_financial.dbsql.FinancialSql;
import com.hz.ys_stock.bean.PresMakings;
import com.hz.ys_system.dao.QueryDao;
import com.sun.net.httpserver.Authenticator.Success;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.hzsearch.action.DynamicAction.java
 *     Description(说明)	: 动态查询检索 action
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description	
 *     			  Jul 29, 2011           韩飞              Created
 *****************************************************************************
 */
public class orderAction extends BaseAction {

	CommonDao cd = new CommonDao();
	OrderDao dao = new OrderDao();
	private Takingsorpay to;
	private static final long serialVersionUID = 1L;
	private Clientinfo c ;
	private Orderinfo o;
	public Orderinfo getO() {
		return o;
	}
	public void setO(Orderinfo o) {
		this.o = o;
	}
	public Clientinfo getC() {
		return c;
	}
	public void setC(Clientinfo c) {
		this.c = c;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public Takingsorpay getTo() {
		return to;
	}
	public void setTo(Takingsorpay to) {
		this.to = to;
	}
	/**
	 * 
	 * @author 吕滋源
	 * @time 2011-11-16	
	 * @return 
	 * 方法功能:模糊查询list
	 * 所属模块:订单管理
	 */
	public String list(){

		String sql = "select o.id as oid,c.id as cid,o.orderId,o.clientId,c.clientName," +
					 "o.name,o.orderTime,o.finishTime,o.balanceTime,o.status " +
					 "from orderinfo as o " +
					 "left join clientinfo as c " +
					 "on o.clientId=c.clientId " ;
//					 "where o.status='未审核' ";
		doAllSearchList(request, "list", sql , "jdbc");
		List list = (List) request.getAttribute("list");
		return SUCCESS;
	}
	/**
	 * 
	 * @author 吕滋源
	 * @time 2011-11-16	
	 * @return 
	 * 方法功能:插入操作
	 * 所属模块:订单管理
	 */
	public String ins(){
		o.setOrderId(new Numbers().getNumbers("A"+DateUtil.getTimek().substring(2, DateUtil.getTimek().length()),"orderId","orderinfo"));//生成的随机编号
		o.setStatus("未审核");
		o.setCreatetime(DateUtil.getDateTime());//插入时间
		cd.saveReturn(o);//插入订单获得id
		fillTaskList(o.getOrderId());
		return SUCCESS;
	}
	/**
	 * 用于填充前台多个name相同的bean
	 * @return
	 */
	private void fillTaskList(String orderid) {
		// TODO Auto-generated method stub
		for(int i=0;i<ttaskName.size();i++){
			Taskinfo t = new Taskinfo();
			if(orderid!=null){
				//如果是插入操作
				t.setOrderid(orderid);
				t.setCreatetime(DateUtil.getDateTime());
				t.setTaskid(new Numbers().getNumbers("T"+DateUtil.getTimek().substring(2, DateUtil.getTimek().length()),"taskid","taskinfo"));
				t.setStatus("未分配机台");
			}
			t.setAddAmount(taddAmount.get(i));
			t.setAmount(tamount.get(i));
			t.setCover(tcover.get(i));
 			t.setCtp(tctp.get(i));
			t.setCut(tcut.get(i));
			t.setPrintSizeType(tprintSizeType.get(i));
			t.setFinishSizeType(tfinishSizeType.get(i));
			t.setDescription(tdescription.get(i));
			t.setFinishSize(tfinishSize.get(i));
			t.setGoodsid(tgoodsid.get(i));
			t.setPrintSize(tprintSize.get(i));
			t.setSingleDouble(tsingleDouble.get(i));
			t.setTaskName(ttaskName.get(i));
			t.setTurnOver(tturnOver.get(i));
			t.setTurnAmount(tturnAmount.get(i));
			t.setSubPaper(tsubPaper.get(i));
			t.setSelfPaper(tselfPaper.get(i));
			cd.saveReturn(t);//插入订单获得id
		}
	}
	/**
	 * 
	 * @author 吕滋源
	 * @time 2011-11-16	
	 * @return 
	 * 方法功能:删除操作
	 * 所属模块:订单管理
	 */
	public String del(){
		String oid=request.getParameter("oid");//订单编号
		cd.DeleteHql("Orderinfo", "id", oid);
		return SUCCESS;
	}
	/**
	 * 
	 * @author 吕滋源
	 * @time 2011-11-16	
	 * @return 
	 * 方法功能:修改操作
	 * 所属模块:订单管理
	 */
	public String upd(){
		Orderinfo oldOrder=(Orderinfo)cd.selHqlObj("FROM Orderinfo where id="+o.getId());
		o.setLastModifyTime(DateUtil.getDateTime());//最后修改时间
		o.setCreatetime(oldOrder.getCreatetime());
		o.setStatus(oldOrder.getStatus());
		o.setCreator(oldOrder.getCreator());
		o.setChecker(oldOrder.getChecker());
		o.setCheckIdea(oldOrder.getCheckIdea());
		o.setCheckTime(oldOrder.getCheckTime());
		o.setDesigner(oldOrder.getDesigner());
		cd.updateinfor(o);//修改订单
		fillTaskListUpd(o.getOrderId());
		return SUCCESS;
	}
	/**
	 * 用于填充前台多个name相同的bean修改
	 * @return
	 */
	private void fillTaskListUpd(String orderid) {
		// TODO Auto-generated method stub
		String deleteSql="delete from taskinfo where orderid='"+orderid+"'";
		List<String> deleteIds= new ArrayList<String>();
		for(int i=0;i<ttaskName.size();i++){
			Taskinfo t = new Taskinfo();
			t.setId(tid.get(i));
			t.setAddAmount(taddAmount.get(i));
			t.setAmount(tamount.get(i));
			t.setCover(tcover.get(i));
 			t.setCtp(tctp.get(i));
			t.setCut(tcut.get(i));
			t.setPrintSizeType(tprintSizeType.get(i));
			t.setFinishSizeType(tfinishSizeType.get(i));
			t.setDescription(tdescription.get(i));
			t.setFinishSize(tfinishSize.get(i));
			t.setGoodsid(tgoodsid.get(i));
			t.setPrintSize(tprintSize.get(i));
			t.setSingleDouble(tsingleDouble.get(i));
			t.setTaskName(ttaskName.get(i));
			t.setTurnOver(tturnOver.get(i));
			t.setTurnAmount(tturnAmount.get(i));
			t.setSubPaper(tsubPaper.get(i));
			t.setSelfPaper(tselfPaper.get(i));
			if(t.getId()==null){
				//如果是插入操作
				t.setOrderid(orderid);
				t.setCreatetime(DateUtil.getDateTime());
				t.setTaskid(new Numbers().getNumbers("T"+DateUtil.getTimek().substring(2, DateUtil.getTimek().length()),"taskid","taskinfo"));
				t.setStatus("未分配机台");
				deleteIds.add(cd.saveReturn(t));//插入订单获得id
			}else{
				//修改操作
				Taskinfo oldTask=(Taskinfo)cd.selHqlObj("FROM Taskinfo where id="+t.getId());
				t.setTaskid(oldTask.getTaskid());
				t.setOrderid(oldTask.getOrderid());
				t.setCreatetime(oldTask.getCreatetime());
				t.setStatus(oldTask.getStatus());
				cd.updateinfor(t);
				deleteIds.add(t.getId()+"");
			}
			for(String id : deleteIds){
				deleteSql+=" and id<>"+id;
				System.out.println(deleteSql);
			}
		}
		System.out.println(deleteSql);
		cd.executeUpdate(deleteSql);
	}
	/**
	 * 
	 * @author 吕滋源
	 * @time 2011-11-16	
	 * @return 
	 * 方法功能:查询单个订单信息
	 * 所属模块:订单管理
	 */
	public String sel(){
		String oid=request.getParameter("oid");//订单编号
		Orderinfo order = (Orderinfo)cd.selHqlObj("FROM Orderinfo where id ="+oid);
		Clientinfo client = (Clientinfo)cd.selHqlObj("FROM Clientinfo where clientId='"+order.getClientId()+"'");
		List<Taskinfo> task = (List<Taskinfo>)cd.selHqlList("FROM Taskinfo where orderid ='"+order.getOrderId()+"'");
		List<PresMakings> pm = null ;
		String goodsids="";
		for(Taskinfo ti : task){
			goodsids+="'"+ti.getGoodsid()+"',";
		}
		
		if(goodsids.length()>1){
			goodsids=goodsids.substring(0,goodsids.length()-1);
			pm=(List<PresMakings>)cd.selHqlList("FROM PresMakings where goodsid in ("+goodsids+")");
		}
		request.setAttribute("order", order);
		request.setAttribute("client", client);
		request.setAttribute("tlist", task);
		request.setAttribute("pm", pm);
		
		return SUCCESS;
	}
	/**
	 * 
	 * @author 吕滋源
	 * @time 2011-11-16	
	 * @return 
	 * 方法功能:查询单个订单信息用于修改
	 * 所属模块:订单管理
	 */
	public String selUpd(){
		String oid=request.getParameter("oid");//订单编号
		Orderinfo order = (Orderinfo)cd.selHqlObj("FROM Orderinfo where id ="+oid);
		Clientinfo client = (Clientinfo)cd.selHqlObj("FROM Clientinfo where clientId='"+order.getClientId()+"'");
		List<Taskinfo> task = (List<Taskinfo>)cd.selHqlList("FROM Taskinfo where orderid ='"+order.getOrderId()+"'");
		List<PresMakings> pm = null ;
		String goodsids="";
		for(Taskinfo ti : task){
			goodsids+="'"+ti.getGoodsid()+"',";
		}
		
		if(goodsids.length()>1){
			goodsids=goodsids.substring(0,goodsids.length()-1);
			pm=(List<PresMakings>)cd.selHqlList("FROM PresMakings where goodsid in ("+goodsids+")");
		}
		request.setAttribute("order", order);
		request.setAttribute("client", client);
		request.setAttribute("tlist", task);
		request.setAttribute("pm", pm);
		
		return SUCCESS;
	}
	
	private List<Integer> tid;
	private List<String> ttaskName;
	private List<String> tgoodsid;
	private List<String> tprintSize;
	private List<String> tfinishSize;
	private List<String> tamount;
	private List<String> taddAmount;
	private List<String> tversionAmount;
	private List<String> tsingleDouble;
	private List<String> tturnOver;
	private List<String> tctp;
	private List<String> tcut;
	private List<String> tcover;
	private List<String> tstatus;
	private List<String> tmid;
	private List<String> tdescription;
	private List<String> ttaskid;
	private List<String> tturnAmount;
	private List<String> tsubPaper;
	private List<String> tselfPaper;
	private List<String> tprintSizeType;
	private List<String> tfinishSizeType;
	public List<Integer> getTid() {
		return tid;
	}
	public void setTid(List<Integer> tid) {
		this.tid = tid;
	}
	public List<String> getTtaskName() {
		return ttaskName;
	}
	public void setTtaskName(List<String> ttaskName) {
		this.ttaskName = ttaskName;
	}
	public List<String> getTgoodsid() {
		return tgoodsid;
	}
	public void setTgoodsid(List<String> tgoodsid) {
		this.tgoodsid = tgoodsid;
	}
	public List<String> getTprintSize() {
		return tprintSize;
	}
	public void setTprintSize(List<String> tprintSize) {
		this.tprintSize = tprintSize;
	}
	public List<String> getTfinishSize() {
		return tfinishSize;
	}
	public void setTfinishSize(List<String> tfinishSize) {
		this.tfinishSize = tfinishSize;
	}
	public List<String> getTamount() {
		return tamount;
	}
	public void setTamount(List<String> tamount) {
		this.tamount = tamount;
	}
	public List<String> getTaddAmount() {
		return taddAmount;
	}
	public void setTaddAmount(List<String> taddAmount) {
		this.taddAmount = taddAmount;
	}
	public List<String> getTversionAmount() {
		return tversionAmount;
	}
	public void setTversionAmount(List<String> tversionAmount) {
		this.tversionAmount = tversionAmount;
	}
	public List<String> getTsingleDouble() {
		return tsingleDouble;
	}
	public void setTsingleDouble(List<String> tsingleDouble) {
		this.tsingleDouble = tsingleDouble;
	}
	public List<String> getTturnOver() {
		return tturnOver;
	}
	public void setTturnOver(List<String> tturnOver) {
		this.tturnOver = tturnOver;
	}
	public List<String> getTctp() {
		return tctp;
	}
	public void setTctp(List<String> tctp) {
		this.tctp = tctp;
	}
	public List<String> getTcut() {
		return tcut;
	}
	public void setTcut(List<String> tcut) {
		this.tcut = tcut;
	}
	public List<String> getTcover() {
		return tcover;
	}
	public void setTcover(List<String> tcover) {
		this.tcover = tcover;
	}
	public List<String> getTstatus() {
		return tstatus;
	}
	public void setTstatus(List<String> tstatus) {
		this.tstatus = tstatus;
	}
	public List<String> getTmid() {
		return tmid;
	}
	public void setTmid(List<String> tmid) {
		this.tmid = tmid;
	}
	public List<String> getTdescription() {
		return tdescription;
	}
	public void setTdescription(List<String> tdescription) {
		this.tdescription = tdescription;
	}
	public List<String> getTtaskid() {
		return ttaskid;
	}
	public void setTtaskid(List<String> ttaskid) {
		this.ttaskid = ttaskid;
	}
	public List<String> getTturnAmount() {
		return tturnAmount;
	}
	public void setTturnAmount(List<String> tturnAmount) {
		this.tturnAmount = tturnAmount;
	}
	public List<String> getTsubPaper() {
		return tsubPaper;
	}
	public void setTsubPaper(List<String> tsubPaper) {
		this.tsubPaper = tsubPaper;
	}
	public List<String> getTselfPaper() {
		return tselfPaper;
	}
	public void setTselfPaper(List<String> tselfPaper) {
		this.tselfPaper = tselfPaper;
	}
	public List<String> getTprintSizeType() {
		return tprintSizeType;
	}
	public void setTprintSizeType(List<String> tprintSizeType) {
		this.tprintSizeType = tprintSizeType;
	}
	public List<String> getTfinishSizeType() {
		return tfinishSizeType;
	}
	public void setTfinishSizeType(List<String> tfinishSizeType) {
		this.tfinishSizeType = tfinishSizeType;
	}
	/**
	 * 
	 * @author 吕滋源
	 * @time 2011-11-16	
	 * @return 
	 * 方法功能:模糊查询list
	 * 所属模块:未审核订单
	 */
	public String uncheckList(){

		String sql = "select o.id as oid,c.id as cid,o.orderId orderid,o.clientId,c.clientName," +
					 "o.name,o.orderTime,o.finishTime,o.balanceTime,o.status " +
					 "from orderinfo as o " +
					 "left join clientinfo as c " +
					 "on o.clientId=c.clientId " +
					 "where o.status='未审核' " +
					 "order by orderTime";
		doAllSearchList(request, "list", sql , "jdbc");
		return SUCCESS;
	}
	/**
	 * 审核减去纸料
	 * @return
	 */
	public String orderCheck(){
		
		//减纸
		//(成品数量+加放数量)/成品开数=纸数    (成品数量+加放数量)/印刷尺寸*单双面{1,2}=转数
		
		String oid = request.getParameter("oid");
		List<Taskinfo> olist = cd.selHqlList("FROM Taskinfo where orderid='"+oid+"'");
		for(Taskinfo t :olist){
			String num=t.getSubPaper();
			if(num==null||num.length()<1||t.getSelfPaper().equals("1")){
				num="0";
			}else{
				cd.executeUpdate("update pres_stock set numnow=numnow-"+num+" where goodsid='"+t.getGoodsid()+"'");
			}
			
			
		}
		cd.executeUpdate("update orderinfo set status='已审核' where orderid='"+oid+"'");
		return SUCCESS;
	}
	/**
	 * @author 修改：韩飞
	 * @time Jan 30, 2012	
	 * @return 
	 * 方法功能: 已完工订单信息
	 */
	public String finishOrderList(){
		String deliver = CommonDao.isnull(request.getParameter("deliver"));
		String carryType = CommonDao.isnull(request.getParameter("carryType"));
		String sql = "select o.id as oid,c.id as cid,o.orderId,o.otherClient,o.otherTel,o.clientId,c.clientName,c.phone," +
			"o.name,o.orderTime,o.finishTime,o.balanceTime,o.status,o.deliverflag,o.delivertime,o.deliveruser,o.debtcash " +
			"from orderinfo as o " +
			"left join clientinfo as c on o.clientId=c.clientId " +
			"where o.status='已完工' ";
		if(deliver.equals("all")){
			
		}else if(deliver.equals("finish")){
			sql += " and o.deliverflag = 'finish'";
		}else{
			sql += " and (o.deliverflag is null or o.deliverflag != 'finish')";
		}
		if("".equals(carryType)){
			sql += " and o.carryType = '发货' ";
			request.setAttribute("carryType", "发货");
		}else{
			sql += " and o.carryType = '"+carryType+"' ";
			request.setAttribute("carryType", carryType);
		}
		//时间条件查询
		sql += new QueryDao().QueryDateInfo(request,"finishTime","begin","end",true);
		
		sql += " order by o.deliverflag,o.ordertime,o.orderid ";
		
		doAllSearchList(request, "list", sql , "jdbc");
		request.setAttribute("deliver", deliver);
		
		return SUCCESS;
	}
	/**
	 * 进入结算页面
	 * @return
	 */
	public String balanceOrder(){
		String oid=request.getParameter("oid");
		Orderinfo order = (Orderinfo) cd.selHqlObj("FROM Orderinfo where id="+oid);
		request.setAttribute("order", order);
		FinancialDao fd = new FinancialDao();
		request.setAttribute("personlist", fd.SelPerson());//查询人员信息
		return SUCCESS;
	}
	/**
	 * 结算
	 * @return
	 * @throws Exception 
	 */
	public String balance() throws Exception{
		DoSavePayMoney(to,"order");
		return SUCCESS;
	}
	
	
	
	/**
	 * @author 韩飞
	 * @time Nov 17, 2011	
	 * @param puser
	 * @return
	 * @throws Exception 
	 * 方法功能: 保存 订单 付款信息
	 */
	public boolean DoSavePayMoney( Takingsorpay to ,String tpflag) throws Exception {
		boolean ins = false;
		CommonDao dbd= new CommonDao();
		if(to!=null){
			to.setTpflag(tpflag);
			to.setCreatetime(DateUtil.getDateTime());
			ins = dbd.insertinfor(to);
			if(ins){
				ins = dbd.insorupd("update orderinfo set status='已结算' where orderid='"+to.getOrderId()+"'");
			}
		}
		return ins;
	}
}
